/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
var chartsPanel = null;
var numberAxis = new YAHOO.widget.NumericAxis();
numberAxis.minimum = 0;
numberAxis.labelFunction = "formatNumber";
numberAxis.alwaysShowZero = false;
numberAxis.calculateByLabelSize = true;

var lowNumberAxis = new YAHOO.widget.NumericAxis();
lowNumberAxis.minimum = 0;
lowNumberAxis.labelFunction = formatLowNumber;

function initializeChartsPanel() {
	chartsPanel = new YAHOO.widget.Panel( "charts-container", {
		width: "785px",  
		x: "20px",
		y: "20px",
		constraintoviewport: false,  
	    underlay: "shadow",  
	    modal: false,
	    close: false,  
	    visible: false,  
	    draggable: false
	} );
	var element = document.getElementById( "current-tab" );
	element = element.innerHTML;
	YAHOO.util.Connect.asyncRequest( 'GET', '../global/get_charts.php?tab=' + element, getChartsContentCallback, null );
	chartsPanel.render();
}

function addChartContainer( title, id, chartType ) {
	var h2 = document.createElement( 'h2' );
	h2.innerHTML = title;
	chartsPanel.body.appendChild( h2 );
	var div = document.createElement( 'div' );
	div.id = id;
	YAHOO.util.Dom.addClass( div, 'chart' );
	YAHOO.util.Dom.addClass( div, chartType );
	div.innerHTML = 'Unable to load Flash content. The charts require Flash Player 9.0.45 or higher. You can install the latest version at the <a target="_blank" href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player Download Center</a>.';
	chartsPanel.body.appendChild( div );
}

var getChartsContentSuccess = function( o ) { 
	//alert( "getChartsContentSuccess( " + o.responseText + " )" );
	chartsPanel.setBody( "" );
	//chartsPanel.show();
	YAHOO.util.Dom.removeClass( document.getElementById( 'charts-container' ), 'hidden' );
	YAHOO.widget.Chart.SWFURL = "../global/yui/charts/assets/charts.swf";
	try {
		var charts = YAHOO.lang.JSON.parse( o.responseText );
		for( i in charts ) {
			for( p in charts[i].options.series ) {
				if( charts[i].options.series[p].color ) {
					charts[i].options.series[p].color = eval( charts[i].options.series[p].color );
				}
			}
			
			if( charts[i].options.rightYAxis && charts[i].options.rightYAxis == 'numberAxis' ) {
				charts[i].options.yAxis.position = "left";
				var axis = new YAHOO.widget.NumericAxis();
				axis.labelFunction = "formatNumber";
				axis.alwaysShowZero = true;
				axis.reverse = true;
				axis.minimum = 1;
				axis.maximum = charts[i].options.rightMaximum;
				axis.calculateByLabelSize = true;
				axis.position = "right";
				axis.order = "secondary";
				if( charts[i].options.rightYAxisTitle ) {
					axis.title = charts[i].options.rightYAxisTitle;
				}

				var yAxis = numberAxis;
				yAxis.position = "left";
				yAxis.order = "primary";
				if( charts[i].options.yAxisTitle ) {
					yAxis.title = charts[i].options.yAxisTitle;
				}

				charts[i].options.yAxes = new Array();
				charts[i].options.yAxes[0] = axis;
				charts[i].options.yAxes[1] = yAxis;
			}
			else if( charts[i].options.yAxis && charts[i].options.yAxis == 'numberAxis' ) {
				charts[i].options.yAxis = numberAxis;
				if( charts[i].options.yAxisTitle ) {
					charts[i].options.yAxis.title = charts[i].options.yAxisTitle;
				}
			}
			
			
			var chartDataSource = new YAHOO.util.DataSource( charts[i].data ); 
			chartDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; 
			chartDataSource.responseSchema = charts[i].responseSchema; 		
			var id = YAHOO.util.Dom.generateId();
			addChartContainer( charts[i].title, id, charts[i].chartType );
			if( charts[i].chartType == 'pie' ) {
				var chart = new YAHOO.widget.PieChart( id, 
						chartDataSource, 
						charts[i].options );
			}
			else if( charts[i].chartType == 'column' ) {
				var chart = new YAHOO.widget.ColumnChart( id, 
						chartDataSource, 
						charts[i].options );
			}
			else if( charts[i].chartType == 'line' ) {
				if( charts[i].options.yAxes ) {
					var chart = new YAHOO.widget.LineChart( id, 
							chartDataSource, 
							{
								series: charts[i].options.series,
								xField: charts[i].options.xField,
								yAxes: charts[i].options.yAxes, 
								style: charts[i].options.style
							} );
				}
				else {
					var chart = new YAHOO.widget.LineChart( id, 
							chartDataSource, 
							charts[i].options );
				}
			}
		}
	} catch ( error ) {
		alert( "There was an error retrieving the chart: \"" + o.responseText + "\"" );
	}
	chartsPanel.show();
}

function formatNumber( value ) {
	return YAHOO.util.Number.format( value, {
		thousandsSeparator: ",",
		decimalPlaces: 0
	});
}

function formatLowNumber( value ) {
	return YAHOO.util.Number.format( value, {
		thousandsSeparator: ",",
		decimalPlaces: 2
	});
}

function formatPercentage( value ) {
	return YAHOO.util.Number.format( value, {
		thousandsSeparator: ",",
		decimalPlaces: 2,
		suffix: '%'
	});
}

function getDataTipTextColumn( item, index, series ) {
	var toolTipText = "";
	if( series ) {
		toolTipText = series.displayName + " "; 
	}
	if( item.name ) {
		toolTipText += item.name;
	}
	
	toolTipText += "\n" + formatNumber( item[series.yField] );
	return toolTipText;
}

function getDataTipTextPie( item, index, series ) {
	var toolTipText = "";
	if( series && series.displayName ) {
		toolTipText = series.displayName + " "; 
	}
	toolTipText += item.name;
	
	toolTipText += "\n" + formatNumber( item.total );
	
	var total = 0;
	for( i in series.data ) {
		total += series.data[i].total;
	}
	if( total > 0 ) {
		var percentage = item.total / total * 100.0;
		toolTipText += "\n" + formatPercentage( percentage );
	}
	
	return toolTipText;
}

function getDataTipTextLine( item, index, series ) {
	var toolTipText = "";
	if( series && series.displayName ) {
		toolTipText = series.displayName + " "; 
	}
	//toolTipText += item.name;
	
	toolTipText += "\n" + formatLowNumber( item[series.yField] );
	return toolTipText;
}

var getChartsContentFailure = function( o ) { 
	alert( "getChartsContentSuccess( " + o.statusText + " )" );
}

var getChartsContentCallback = {
	success: getChartsContentSuccess,
	failure: getChartsContentFailure
};